NB.ns("app").share = (function() {
    function init(){
        
    }

	function browserVersion(){
    	var u = navigator.userAgent, app = navigator.appVersion;
    	var ua = navigator.userAgent.toLowerCase();
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); 
        var href = document.location.href;
        var strs = href.split("channel=")[1];
        doShare();
        //判断是在微信中打开
        var regex = /MicroMessenger/i;
        if(regex.test(ua)){
            $('#winxinShow').show();
            return;
        }
        if(isiOS == true){
        	document.location = 'https://itunes.apple.com/app/id1277470059';
        	$('#button_id').attr("href","https://itunes.apple.com/app/id1277470059");
            return;
        }
        if(isAndroid = true){
        	window.location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=com.ms.flowerlive";
        	$('#button_id').attr("href","http://a.app.qq.com/o/simple.jsp?pkgname=com.ms.flowerlive");
            return;
        }
    }

    function doShare(){
        var code = $("#biao").val();
        if(code){
            $.ajax({
                type : "GET",
                url : "http://test.huakai.me/api/app/saveShareCode",
                data : {"code":code},
                dataType : "jsonp",
                async : false,
                jsonp : "jsonpCallback",
                beforeSend: function(xhr){ //发送请求前调用，可以放一些"正在加载"之类额话
                    // xhr.setRequestHeader("x-access-id", shareId);
                    // 加载层-默认风格
                    layer.load();
                    // layer.msg("正在加载……");
                },
                success: function (data) {   //成功后回调
                    //此处演示关闭
                    layer.closeAll('loading');
                }
            });
        }else{
            // layer.msg('输入的手机号码不正确');
        }
    }
    
    return {
        init : init,
        doShare : doShare,
    	browserVersion : browserVersion
    }
})()

$(function(){
    // 加载层-默认风格
    layer.load();

    var url = decodeURIComponent(window.location.href);
    var paraString = url.substring(url.indexOf('=') + 1, url.length).split('&');
    //alert(paraString);
    $("#biao").val(paraString);

	$('#winxinShow').click(function(){
    	$(this).hide();	
    });
	new function (){
		var _self = this;
		_self.width = 640;//设置默认最大宽度
		_self.fontSize = 20;//默认字体大小
		_self.widthProportion = function(){var p = (document.body&&document.body.clientWidth||document.getElementsByTagName("html")[0].offsetWidth)/_self.width;return p>1?1:p<0.5?0.5:p;};
		_self.changePage = function(){
			document.getElementsByTagName("html")[0].setAttribute("style","font-size:"+_self.widthProportion()*_self.fontSize+"px !important");
		}
		_self.changePage();
		window.addEventListener('resize',function(){_self.changePage();},false);
	};
    //getShareCode();
    var clipboard = new Clipboard("#button_id");
    // clipboard.destroy();

    clipboard.on("success",function (element) {//复制成功的回调
        clipboard.destroy();
    });

    clipboard.on("error",function (element) {//复制失败的回调
        clipboard.destroy();
    });

    //此处演示关闭
    layer.closeAll('loading');

});

function getShareCode(){
    var shareId = $("#biao").val();
    if(shareId){
        $.ajax({
            type : "GET",
            url : "http://test.huakai.me/api/app/getShareCode",
            data : {"shareId":shareId},
            //headers: {'Authorization': 'Basic ' + authKey, "x-access-id":shareId},
            dataType : "jsonp",
            async : false,
            jsonp : "jsonpCallback",
            beforeSend: function(xhr){ //发送请求前调用，可以放一些"正在加载"之类额话
                // xhr.setRequestHeader("x-access-id", shareId);
                // 加载层-默认风格
                layer.load();
                // layer.msg("正在加载……");
            },
            success: function (data) {   //成功后回调
                if (data.code == 0) {
                    //此处演示关闭
                    layer.closeAll('loading');
                    //alert(data.data);
                    $("#biao").val(data.data);
                } else if(data.code != -1) {
                    // layer.msg('验证码发送失败');
                    // getShareCode();
                    layer.msg(data.message);
                }
            },
            error: function(data){ //失败后回调
                //getShareCode();
            }
        });
    }else{
        layer.msg('错误url');
    }
}